package com.am.mosl.cityandroid.leftmenu;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.bean.MData;


public class MessageListAdapter extends BaseAdapter {

	private List<MData> mData = new ArrayList<MData>();
	private LayoutInflater inflater;

	public MessageListAdapter(Context context, List<MData> data) {
		if (this.mData != null) {
			((List<MData>) this.mData).clear();
		}

		mData.addAll(data);
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;
		final MData data = mData.get(position);
		if (convertView == null) {
			holder = new ViewHolder();

			convertView = inflater.inflate(R.layout.message_item, null);

			holder.title = (TextView) convertView
					.findViewById(R.id.message_item_title);
			holder.message = (TextView) convertView
					.findViewById(R.id.message_item_content);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.title.setText(data.getMessage_title());
		holder.message.setText(data.getMessage_description());

		return convertView;
	}

	class ViewHolder {
		public TextView title;
		public TextView message;

	}

	// 在后面添加
	public void append(List<MData> data) {
		if (data == null) {
			return;
		}
		mData.addAll(data);
		notifyDataSetChanged();
	}

	public MData getData(int position) {

		MData data = mData.get(position);
		return data;

	}

}
